package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.CreditPeopleType;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.commpent.mapper.yw.YwBusinessBuyerVoMapper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.dh.IDhRemindLoanService;
import com.zjdiepu.www.ibiz.dz.IDzLoanMiddleBankService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessBuyerService;
import com.zjdiepu.www.ibiz.zpp.PublicService;
import com.zjdiepu.www.model.dh.dto.DhRemindLoan;
import com.zjdiepu.www.model.dh.ext.DhRemindLoanExt;
import com.zjdiepu.www.model.dz.dto.DzLoanMiddleBank;
import com.zjdiepu.www.model.yw.dto.YwBusinessBuyer;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.ext.YwBusinessBuyerExt;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 放款还款提醒View Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月11日 20时36分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "DhRemindLoanViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"放款还款提醒"}, produces="text/html;charset=UTF-8")
public class DhRemindLoanViewController extends BaseController {
    @Resource
    private IDhRemindLoanService dhRemindLoanService;
    @Resource
    private IYwBusinessBuyerService ywBusinessBuyerService;
    @Resource
    private IDzLoanMiddleBankService dzLoanMiddleBankService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private YwBusinessBuyerVoMapper ywBusinessBuyerVoMapper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private PublicService publicService;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: dhRemindRemitDetail  <br/>
     * @Description: 放款提醒详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "放款提醒详情", response = Boolean.class, httpMethod="GET", tags={"放款提醒"})
    @RequestMapping(value = "dhRemindRemitDetail", method = RequestMethod.GET)
    public String dhRemindRemitDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	
    	model.addAttribute("busiId", id);
    	//下拉选项
    	model.addAttribute("loanRemindResultType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.LOAN_REMIND_RESULT_TYPE,"请选择..."));
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    	}
    	
    	DzLoanMiddleBank dzLoanMiddleBank = new DzLoanMiddleBank();
    	dzLoanMiddleBank.setBusiId(id);
    	dzLoanMiddleBank.setDpStatus(DpStatus.NORMAL.getCode());
    	dzLoanMiddleBank = dzLoanMiddleBankService.selectRefined(dzLoanMiddleBank);
    	model.addAttribute("dzLoanMiddleBank", dzLoanMiddleBank);
    		
    	DhRemindLoan dhRemindLoan = new DhRemindLoan();
    	dhRemindLoan.setDpStatus(DpStatus.NORMAL.getCode());
    	dhRemindLoan.setBusiId(id);
    	dhRemindLoan = dhRemindLoanService.selectRefined(dhRemindLoan);
    	if(null != dhRemindLoan) {
    		model.addAttribute("dhRemindLoan", dhRemindLoan);
    		
    		Date thisTimeLending = dhRemindLoan.getTimeLending();
    		String thisId = dhRemindLoan.getId();
    		String nextBusiId = dhRemindLoanService.getNextBusiId(thisTimeLending, thisId);
    		model.addAttribute("nextBusiId", nextBusiId);
    		String previousBusiId = dhRemindLoanService.getPreviousBusiId(thisTimeLending, thisId);
        	model.addAttribute("previousBusiId", previousBusiId);
    	}
    	
    	YwBusinessBuyer ywBusinessBuyerParam = new YwBusinessBuyer();
    	ywBusinessBuyerParam.setBusiId(id);
    	ywBusinessBuyerParam.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessBuyerParam.setCarManType(CreditPeopleType.ZHU_JIE_REN.getCode());
    	YwBusinessBuyer ywBusinessBuyer = ywBusinessBuyerService.selectRefined(ywBusinessBuyerParam);
    	if(null != ywBusinessBuyer) {
    		model.addAttribute("hasBuyer",true);
    		ywBusinessBuyerVoMapper.mapper(ywBusinessBuyer);
    		model.addAttribute("ywBusinessBuyer", ywBusinessBuyer);	
    	}
    	
    	ywBusinessBuyerParam.setCarManType(CreditPeopleType.PEI_OU.getCode());
    	YwBusinessBuyer ywBusinessBuyerPeiou = ywBusinessBuyerService.selectRefined(ywBusinessBuyerParam);
    	if(null != ywBusinessBuyerPeiou) {
    		model.addAttribute("hasGtBuyer",true);
    		ywBusinessBuyerVoMapper.mapper(ywBusinessBuyerPeiou);
    		model.addAttribute("ywBusinessGtBuyer", ywBusinessBuyerPeiou);	
    	}
    	
    	YwBusinessBuyerExt ywBusinessBuyerExtParam = new YwBusinessBuyerExt();
    	ywBusinessBuyerExtParam.setBusiId(id);
    	ywBusinessBuyerExtParam.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessBuyerExtParam.setCarManType(CreditPeopleType.DAN_BAO_REN.getCode());
    	ywBusinessBuyerExtParam.setRows(4);
    	List<YwBusinessBuyer> ywBusinessBuyerDanBaoRens = ywBusinessBuyerService.selectByExt(ywBusinessBuyerExtParam);
    	if(CollectionUtils.isNotEmpty(ywBusinessBuyerDanBaoRens)) {
    		ywBusinessBuyerVoMapper.mapper(ywBusinessBuyerDanBaoRens);
    		for(int i = 0; i < ywBusinessBuyerDanBaoRens.size(); i++) {
    			YwBusinessBuyer ywBusinessBuyerDanBaoRen = ywBusinessBuyerDanBaoRens.get(i);
    			model.addAttribute("hasDbBuyer" + (i + 1), true);
    			model.addAttribute("danbaoren" + (i + 1) , ywBusinessBuyerDanBaoRen);	
    		}
    	}
    	
    	return "system/loanAfter/dhRemindRemitDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: dhRemindRefundDetail  <br/>
     * @Description: 还提醒详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "还提醒详情", response = Boolean.class, httpMethod="GET", tags={"还款提醒"})
    @RequestMapping(value = "dhRemindRefundDetail", method = RequestMethod.GET)
    public String dhRemindRefundDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	
    	model.addAttribute("busiId", id);
    	//下拉选项
    	model.addAttribute("loanRemindResultType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.REPAYMENT_REMIND_RESULT_TYPE,"请选择..."));
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    	}
    	
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    	}
    	
    	DzLoanMiddleBank dzLoanMiddleBank = new DzLoanMiddleBank();
    	dzLoanMiddleBank.setBusiId(id);
    	dzLoanMiddleBank.setDpStatus(DpStatus.NORMAL.getCode());
    	dzLoanMiddleBank = dzLoanMiddleBankService.selectRefined(dzLoanMiddleBank);
    	model.addAttribute("dzLoanMiddleBank", dzLoanMiddleBank);
    	
    	DhRemindLoanExt dhRemindLoanExt = new DhRemindLoanExt();
    	dhRemindLoanExt.setBaseWhere();
    	dhRemindLoanExt.setRows(1);
    	dhRemindLoanExt.setBusiId(id);
    	List<DhRemindLoan> dhRemindLoans = dhRemindLoanService.selectByExt(dhRemindLoanExt);
    	if(CollectionUtils.isNotEmpty(dhRemindLoans)) {
    		DhRemindLoan dhRemindLoan = dhRemindLoans.get(0);
    		model.addAttribute("dhRemindLoan", dhRemindLoan);
    		
    		Date thisTimeLending = dhRemindLoan.getTimeLending();
    		String thisId = dhRemindLoan.getId();
    		String nextBusiId = dhRemindLoanService.getNextBusiId(thisTimeLending, thisId);
    		model.addAttribute("nextBusiId", nextBusiId);
    		String previousBusiId = dhRemindLoanService.getPreviousBusiId(thisTimeLending, thisId);
        	model.addAttribute("previousBusiId", previousBusiId);
    	}
    	
    	YwBusinessBuyer ywBusinessBuyer = new YwBusinessBuyer();
    	ywBusinessBuyer.setBusiId(id);
    	ywBusinessBuyer.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessBuyer.setCarManType(CreditPeopleType.ZHU_JIE_REN.getCode());
    	ywBusinessBuyer = ywBusinessBuyerService.selectRefined(ywBusinessBuyer);
    	if(null != ywBusinessBuyer) {
    		model.addAttribute("hasBuyer",true);
    		ywBusinessBuyerVoMapper.mapper(ywBusinessBuyer);
    		model.addAttribute("ywBusinessBuyer", ywBusinessBuyer);	
    	}
    	YwBusinessBuyer ywBusinessBuyer_ = new YwBusinessBuyer();
    	ywBusinessBuyer_.setBusiId(id);
    	ywBusinessBuyer_.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessBuyer_.setCarManType(CreditPeopleType.PEI_OU.getCode());
    	ywBusinessBuyer_ = ywBusinessBuyerService.selectRefined(ywBusinessBuyer_);
    	if(null != ywBusinessBuyer_) {
    		model.addAttribute("hasGtBuyer",true);
    		ywBusinessBuyerVoMapper.mapper(ywBusinessBuyer_);
    		model.addAttribute("ywBusinessGtBuyer", ywBusinessBuyer_);	
    	}
    	
    	YwBusinessBuyerExt ywBusinessBuyerExtParam = new YwBusinessBuyerExt();
    	ywBusinessBuyerExtParam.setBusiId(id);
    	ywBusinessBuyerExtParam.setDpStatus(DpStatus.NORMAL.getCode());
    	ywBusinessBuyerExtParam.setCarManType(CreditPeopleType.DAN_BAO_REN.getCode());
    	ywBusinessBuyerExtParam.setRows(4);
    	List<YwBusinessBuyer> ywBusinessBuyerDanBaoRens = ywBusinessBuyerService.selectByExt(ywBusinessBuyerExtParam);
    	if(CollectionUtils.isNotEmpty(ywBusinessBuyerDanBaoRens)) {
    		ywBusinessBuyerVoMapper.mapper(ywBusinessBuyerDanBaoRens);
    		for(int i = 0; i < ywBusinessBuyerDanBaoRens.size(); i++) {
    			YwBusinessBuyer ywBusinessBuyerDanBaoRen = ywBusinessBuyerDanBaoRens.get(i);
    			model.addAttribute("hasDbBuyer" + (i + 1), true);
    			model.addAttribute("danbaoren" + (i + 1) , ywBusinessBuyerDanBaoRen);	
    		}
    	}
    	
    	return "system/loanAfter/dhRemindRefundDetail";
    }
    
}
